<template>
  <div class="app-bg auto">
    <div  class="app-list">
    <div  style="color: #00B9F1;"  >
      <span class="col-xs-2">户号：</span><span class="col-xs-4">{{data.f_userinfo_id}}</span>
      <span class="col-xs-2">姓名：</span><span class="col-xs-4">{{data.f_user_name}}</span>
    </div>
    <div >
      <span class="col-xs-3">电话：</span><span class="col-xs-9">{{data.f_user_phone}}</span>
    </div>
    <div >
      <span class="col-xs-3">地址：</span><span class="col-xs-9">{{data.f_address}}</span>
    </div>
    </div>

    <div v-for='row in user' class="app-list">
        <div >
          <span class="col-xs-2">表号：</span><span class="col-xs-4">{{row.f_user_id}}</span>
          <span class="col-xs-3">表位置：</span><span class="col-xs-3">{{row.f_position}}</span>
        </div>
        <div >
          <span class="col-xs-3">抄表状态：</span><span class="col-xs-3">{{row.f_meter_state}}</span>
          <span class="col-xs-3">缴费状态：</span><span class="col-xs-3">{{row.f_whether_pay}}</span>
        </div>
        <div >
          <span class="col-xs-3">上期指数：</span><span class="col-xs-3">{{row.f_last_tablebase}}</span>
          <span class="col-xs-3">本期指数：</span><span class="col-xs-3"><input v-model='row.f_tablebase' style="width: 60px;"></input></span>
        </div>
        <div>
          <span class="col-xs-3">用气量：</span><span class="col-xs-9">{{ row.f_tablebase - row.f_last_tablebase}}</span>
        </div>
        <div>
          <span class="col-xs-3">抄表时间：</span><span class="col-xs-9">{{row.f_hand_date}}</span>
        </div>
        <div class="row" style="margin-top:10px;">
          <label  class=" col-sm-2 control-label">备注:</label>
          <div class="col-sm-9">
            <textarea style="width:100%" class="form-control"  rows="3"
            placeholder="备注" v-model='row.f_remark'></textarea>
          </div>
        </div>
      </div>

    <div class="app-btn">
      <button type="button" name="button" class="btn btn-primary"  @click="$back()">
        <span  class="glyphicon glyphicon-remove-circle" style="margin-right: 15px;"></span>返回
      </button>
      <button type="button" name="button" class="btn btn-primary"  @click="search()">
        <span  class="glyphicon glyphicon-zoom-in" style="margin-right: 15px;"></span>保存
      </button>
    </div>
  </div>
</template>

<script>
import co from 'co'
import * as Util from '../../components/Util'
import appData from '../../components/appData'
import config from '../../components/config'

let searchqiliang = function * (self){
  let condition = `1=1 and h.f_userinfo_id = '${self.data.f_userinfo_id}' and h.f_hand_date = '${self.data.f_max_date}' `
  if(self.data.f_meter_state === '已抄表'){
    condition += `and f_meter_state = '已抄表' and f_whether_pay = '否'`
  }else if(self.data.f_meter_state === '未抄表'){
      condition += `and f_meter_state = '未抄表' and f_whether_pay = '否'`
  }
  yield self.$resetpost(config.resultUrl + 'rs/sql/searchqiliang', {data: {condition:condition}},
   {resolveMsg: null, rejectMsg: null})
  .then ((res)=> {
    self.user = res.data
  })
  .catch ((res)=> {
    self.$showMessage('获取用户数据失败，请查看你输入的查询信息')
  })
}
let savetablebase = function * (self){
  //config.resultUrl +
  yield self.$resetpost(config.resultUrl + 'rs/logic/savetablebase', {data:{user:self.user,f_input_date:self.model.f_input_date,f_end_date:self.model.f_end_date}},
  {resolveMsg: null, rejectMsg: null})
  .then ((res)=> {
    self.$showMessage('保存成功，已划价')
    self.$back()
  })
  .catch ((res)=> {
    self.$showMessage('保存失败')
  })
}

export default {
  title:'划价',
  props:['data'],
  data(){
    return{
      model:{
        f_position:'',
        f_input_date : Util.toStandardTimeString(),
        f_end_date : Util.addDate (Util.toStandardTimeString(), 60)
      },
      user:[]
    }
  },
  methods: {
    search(){
      this.user.forEach((msg) => {
        if (msg.f_tablebase - msg.f_last_tablebase < 0) {
          this.$showMessage('本期指数不能小于上期指数')
          return
        }else{
          let gen = savetablebase(this)
          return co(gen)
        }
      })
    }
  },
  ready () {
    let gen = searchqiliang(this)
    return co(gen)
  }
}
</script>
